package org.quasar.airNova.presentationLayer;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.quasar.airNova.businessLayer.CalendarDate;
import org.quasar.airNova.businessLayer.CartaoCredito;
import org.quasar.airNova.businessLayer.Cliente;
import org.quasar.airNova.businessLayer.Reserva;

public class AssociarBilheteReserva extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField nLugarJTF;
	private Gui gui;
	private JComboBox nReserva;
	private JComboBox nCliente;
	private JComboBox nVoo;
	private JComboBox cbDatas;

	/**
	 * Create the panel.
	 * 
	 * @param gui
	 */
	public AssociarBilheteReserva(Gui gui) {
		setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("Voo");
		lblNewLabel.setBounds(40, 76, 151, 14);
		panel.add(lblNewLabel);

		JLabel lblNewLabel_2 = new JLabel("Reserva");
		lblNewLabel_2.setBounds(40, 148, 126, 14);
		panel.add(lblNewLabel_2);

		JLabel lblNewLabel_1 = new JLabel("Cliente");
		lblNewLabel_1.setBounds(40, 123, 126, 14);
		panel.add(lblNewLabel_1);

		JLabel lblLugar = new JLabel("Lugar");
		lblLugar.setBounds(40, 173, 126, 14);
		panel.add(lblLugar);

		nLugarJTF = new JTextField();
		nLugarJTF.setBounds(231, 170, 203, 20);
		panel.add(nLugarJTF);
		nLugarJTF.setColumns(10);

		JButton btnNewButton_2 = new JButton("Apagar Campos");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nLugarJTF.setText("");
			}
		});
		btnNewButton_2.setBounds(251, 217, 126, 23);
		panel.add(btnNewButton_2);

		JButton btnNewButton = new JButton("Submeter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (nReserva.getSelectedItem() != null
						&& nCliente.getSelectedItem() != null
						&& nVoo.getSelectedItem() != null
						&& nLugarJTF.getText().length() != 0
						&& cbDatas.getSelectedItem() != null) {
					AirNova_Database.associarBilheteReserva(
							(String) nReserva.getSelectedItem(),
							(String) nCliente.getSelectedItem(),
							(String) nVoo.getSelectedItem(),
							nLugarJTF.getText(),
							(String) cbDatas.getSelectedItem());
				}
			}
		});
		btnNewButton.setBounds(44, 217, 98, 23);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Cancelar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(
						getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(152, 217, 89, 23);
		panel.add(btnNewButton_1);

		JLabel lblNewLabel_3 = new JLabel("Associar Bilhetes a uma reserva");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 243, 32);
		panel.add(lblNewLabel_3);

		nReserva = new JComboBox();
		nReserva.setBounds(231, 145, 203, 20);
		panel.add(nReserva);

		nCliente = new JComboBox(Tools.getClienteCodigos());
		nCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				generateComboBoxReservas((String) nCliente.getSelectedItem());
			}

		});
		nCliente.setBounds(231, 120, 203, 20);
		panel.add(nCliente);

		if (Tools.getVoosNumero() != null)
			nVoo = new JComboBox(Tools.getVoosNumero());
		else
			nVoo = new JComboBox();

		nVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				generateComboxData((String) nVoo.getSelectedItem());
			}
		});
		nVoo.setBounds(231, 73, 203, 20);
		panel.add(nVoo);

		JLabel datas = new JLabel("Datas");
		datas.setBounds(40, 98, 151, 14);
		panel.add(datas);

		cbDatas = new JComboBox();
		cbDatas.setBounds(231, 95, 203, 20);
		panel.add(cbDatas);

	}

	public Gui getGui() {
		return gui;
	}

	private void generateComboxData(String nVoo) {
		CalendarDate[] cds = Tools.getVoosData(nVoo);
		addDatasComboBox(cds);
	}

	private void addDatasComboBox(CalendarDate[] cds) {
		cbDatas.removeAllItems();
		for (int i = 0; i < cds.length; i++) {
			cbDatas.addItem("" + cds[i].day() + "-" + cds[i].month() + "-"
					+ cds[i].year());
		}
	}

	private void generateComboBoxReservas(String nCliente) {
		Set<Reserva> reservas = Tools.getAllReservasCliente(nCliente);
		nReserva.removeAllItems();
		for (Reserva each : reservas) {
			nReserva.addItem("" + each.n_reserva());
		}
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}
}
